/**
 * @author PerronJones
 */

var mlist = 0;
var i = 2;
//  set your counter for imgLoop() to 2
var anchorID = ""//initialize anchorID for imgLoop functions

var intervalHandle = 0;

$(document).ready(function() {

	//navOperate();
	//navOperate();
	var curpage = window.location.href;
	//checks if we are on the home page
	if (curpage.indexOf("Home") != -1) {
		$('.bgImg').hide();
		mlist = document.getElementById("imageSelector").getElementsByTagName("li").length;
		imgOperate();
		if (mlist > 0) {
			imgLoop();
		}

	};

});

// Performs the background image rotations in Home page
function imgOperate() {
	$('.bgImg').hide();
	$('#imageSelector a').bind('click', function(e) {
		$('#imageSelector a.current').removeClass('current');
		$('.bgImg:visible').fadeOut(1000);
		//$('.bgImg:visible').hide();
		$(this.hash).fadeIn(2000);
		$(this).addClass('current');
		var chosen = (this.id.split("img"))[1]
		//increments counter to show next slide when it enters imgLoop()
		i = parseInt(chosen) + 1
		counter = 0;
		//reset counter/timer
		e.preventDefault();
	}).filter(':first').click();
};

var counter = 0;
function imgLoop() {//  create a loop function
	window.setInterval(function() {//Sets interval for every .5 seconds
		counter++;
		//console.log("counter = %d ", counter);
		if (counter == 8) {//this checks if 4 seconds has elapses 4 secs/.5 secs = 8

			if (i > mlist) {
				i = 1;
			}
			anchorId = "#img" + i.toString();
			$(anchorId).click();
		}
	}, 500);
};

// Performs the background image rotations in Work page in main top area
function imgOperate2() {
	$('.bgImg2').hide();
	$('#imageSelector a').bind('click', function(e) {
		$('#imageSelector a.current').removeClass('current');
		//$('.bgImg2:visible').fadeOut(1000);
		$('.bgImg2:visible').hide();
		$(this.hash).fadeIn(1000);
		$(this).addClass('current');
		var chosen = (this.id.split("_"))[1]
		//increments counter to show next slide when it enters imgLoop()
		i = parseInt(chosen) + 1
		counter2 = 0;
		//reset counter/timer
		e.preventDefault();
	}).filter(':first').click();
};

var counter2 = 0;
function imgLoop2() {//  create a loop function

	intervalHandle = window.setInterval(function() {//Sets interval for every .5 seconds
		counter2++;
		//console.log("counter2 = %d", counter2);
		if (counter2 == 8) {//this checks if 4 seconds has elapses 4 secs/.5 secs = 8
			if (i > mlist) {
				i = 1;
			}
			anchorId = "#anchor_" + i.toString();
			$(anchorId).click();
		}
	}, 500);
};

function changeProject(selection) {
	window.clearInterval(intervalHandle);
	changePrjDesc(selection);
	imgOperate2();
	mlist = document.getElementById("imageSelector").getElementsByTagName("li").length;
	if (mlist > 0) {
		imgLoop2();
	}

};

// Performs the background image rotations in Work page in main top area
function changePrjDesc(selection) {
	$('.projDesc').hide();
	//Hide the project descriptions
	var chosen = (selection.id.split("_"))[1]
	var prjDesc = "#projDesc_" + chosen
	$(prjDesc).fadeIn(1000);
	$('#exitprj').fadeIn(1000);
	
};

// Performs the background image rotations in Work page in main top area
function defaultSlideshow() {
	window.clearInterval(intervalHandle);

	mlist = document.getElementById("imageSelector").getElementsByTagName("li").length;

	imgOperate2();
	if (mlist > 0) {
		imgLoop2();
	}
};

